home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / castools.zip / FAXGETE3.C < prev    next >
Text File  |  1990-01-12  |  6KB  |  172 lines

  1.  
  2.  
  3. /*
  4.    FAXGETE3.C  A high-level CAS Toolkit function.
  5.  
  6.    This function gets one or more File Transfer Records (FTR's) of an event.
  7.  
  8.    INPUT:  Required: An event handle, the number of the first FTR to retrieve,
  9.                      and the number of FTR's to retrieve.
  10.            Optional: A queue number and a pointer to a list of FTR structures.
  11.  
  12.    OUTPUT: Pointer to a list of File Transfer Record structures, the
  13.            queue in which the event was found, and the number of FTR's
  14.            actually retrieved.
  15. */
  16.  
  17. #include <stdlib.h>
  18. #include <io.h>
  19. #include <stdio.h>
  20. #include <malloc.h>
  21. #include <cas.h>
  22. #include <fax.h>
  23.  
  24. FTRLIST * pascal FAXGetEventFileInfo(int EventHandle,
  25.                                      BYTE *queue,
  26.                                      FTRLIST *FTRs,
  27.                                      int WhichFTRFirst,
  28.                                      int *HowManyFTRs)
  29. {
  30.   int FileHandle,                 /* returned from CASFind functions */
  31.       retval,                     /* for return value of CAS calls */
  32.       red;                        /* bytes or FTR's read with the read() */
  33.   int i;                          /* just for counting */
  34.   long sought;                    /* an honest woman... */
  35.   ECF ControlInfo;                /* need FTROffset to find FTRs */
  36.   FTRLIST *CurrFTRL,              /* for stepping down the linked list */
  37.           *FTRLhead;              /* pointer to head of list */
  38.   CECS CurrentEventInfo;
  39.  
  40.   FAXerrno = CASerrorcode = 0;      /* They keep it if nothing goes wrong */
  41.  
  42.   /* If the event is currently executing, can't get just any file info, */
  43.   /*   but can get the info for the currently transmitting file.  For that, */
  44.   /*   use the function CASGetCurrentEventStatus function directly. */
  45.   if (EventHandle == CASGetCurrentEventStatus(&CurrentEventInfo)) {
  46.     FAXerrno = EVENTISCURRENT;
  47.     return(NULL);
  48.   }
  49.  
  50.   /* If no queue specified, search all the queues for the given Event Handle */
  51.   if (*queue == UNKNOWN_QUEUE) {
  52.     for (*queue = TASK_QUEUE; *queue <= LOG_QUEUE; (*queue)++) {
  53.       retval = CASFindFirst(ANY_STATUS, SEARCH_FORWARD, *queue);
  54.       while (retval && (retval != EventHandle)) {
  55.         if (retval > 0) {           /* found an event, but not the one sought */
  56.           retval = CASFindNext(*queue);       /* so find next one */
  57.         }
  58.         else break;                 /* end of queue, or other error */
  59.       }
  60.       if (retval == EventHandle) break;           /* else go to next queue */
  61.     }                                             /* Done with all the queues */
  62.     if (retval != EventHandle) {       /* after all that, just give up! */
  63.       *queue = UNKNOWN_QUEUE;
  64.       FAXerrno = EVENTNOTFOUND;
  65.       CASerrorcode = -retval;
  66.       return(NULL);
  67.     }
  68.   }
  69.  
  70.   /* To get to here, the queue was given, or we found it anyway */
  71.   retval = CASOpenFile(EventHandle, 0, *queue);
  72.   if (retval < 0) {                            /* CAS error, get out */
  73.     FAXerrno = OPENFILE;
  74.     CASerrorcode = -retval;
  75.     return(NULL);
  76.   }
  77.   FileHandle = retval;
  78.  
  79.   /* First, read the Event Control File to know the number of FTR's */
  80.   red = read(FileHandle, (char *)&ControlInfo, sizeof(ECF));
  81.   if (red != sizeof(ECF)) {
  82.     FAXerrno = CANTREADFILE;
  83.     return(NULL);
  84.   }
  85.  
  86.   /* Next, seek to beginning of FTR's */
  87.   sought = lseek(FileHandle, (long)ControlInfo.FTROffset, SEEK_SET);
  88.   if (sought == -1L) {
  89.     FAXerrno = LSEEKERROR;
  90.     return(NULL);
  91.   }
  92.  
  93.   /* Check that starting point is within range */
  94.   if (WhichFTRFirst >= ControlInfo.FileCount) {
  95.     FAXerrno = NOTTHATMANYFILES;
  96.     return(NULL);
  97.   }
  98.  
  99.   /* "0" means get ALL of them, from WhichFTRFirst on... */
  100.   if (*HowManyFTRs == 0) {
  101.     *HowManyFTRs = ControlInfo.FileCount - WhichFTRFirst;
  102.   }
  103.  
  104.   /* Advance to FTR indicated by parameter WhichFTRFirst */
  105.   if ((WhichFTRFirst + *HowManyFTRs) > ControlInfo.FileCount) {
  106.     FAXerrno = NOTTHATMANYFILES;
  107.     return(NULL);
  108.   }
  109.   else {
  110.     sought = lseek(FileHandle, (long)WhichFTRFirst * sizeof(FTR), SEEK_CUR);
  111.     if (sought == -1L) {
  112.       FAXerrno = LSEEKERROR;
  113.       return(NULL);
  114.     }
  115.   }
  116.  
  117.   /* Decide whether to allocate the linked list or use the caller's */
  118.   if (FTRs) {
  119.     CurrFTRL = FTRLhead = FTRs;
  120.   }
  121.   else {
  122.     CurrFTRL = FTRLhead = (FTRLIST *)calloc(1, sizeof(FTRLIST));
  123.     if (!CurrFTRL) {
  124.       FAXerrno = OUTOFMEMORY;
  125.       *HowManyFTRs = 0;
  126.       return(NULL);
  127.     }
  128.     for (i=1; i<*HowManyFTRs; i++) {
  129.       CurrFTRL->next = (FTRLIST *)calloc(1, sizeof(FTRLIST));
  130.       if (!CurrFTRL->next) {
  131.         FAXerrno = OUTOFMEMORY;
  132.         *HowManyFTRs = 0;
  133.         free_FTRLIST(FTRLhead);
  134.         return(NULL);
  135.       }
  136.       CurrFTRL = CurrFTRL->next;
  137.     }
  138.     CurrFTRL = FTRLhead;
  139.   }
  140.  
  141.   /* Having reached WhichFTRFirst, read in the number of FTR's requested */
  142.   for (i = 0;
  143.        i < *HowManyFTRs;
  144.        CurrFTRL = CurrFTRL->next, i++) {
  145.  
  146.     if (eof(FileHandle)) {
  147.       FAXerrno = UNEXPECTEDEOF; /* EOF, but there should have been more FTR's */
  148.       close(FileHandle);
  149.       *HowManyFTRs = i;
  150.       if (!FTRs) {
  151.         free_FTRLIST(CurrFTRL);
  152.       }
  153.       return(FTRLhead);
  154.     }
  155.     red = read(FileHandle, (char *)&CurrFTRL->OneFTR, sizeof(FTR));
  156.     if (red != sizeof(FTR)) {
  157.       FAXerrno = CANTREADFILE;
  158.       *HowManyFTRs = i;
  159.       if (!FTRs) {
  160.         free_FTRLIST(CurrFTRL);
  161.       }
  162.       return(FTRLhead);
  163.     }
  164.     memset(CurrFTRL->OneFTR.RESERVED, 0, FTRRESERVED);
  165.   }
  166.   if (close(FileHandle) == -1) {
  167.     FAXerrno = CANTCLOSEFILE;          /* Warning only, we got what we wanted */
  168.   }
  169.   *HowManyFTRs = i;
  170.   return(FTRLhead);
  171. }
  172.